package com.dimld.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeFun {
    /**
     * 得到本周的星期1-7的日期
     *
     * @param Week int
     * @return String
     */
    public static String getWeekDate(int Week) {
        String   str;
        Calendar cr = Calendar.getInstance(); // 当时的日期和时间
        cr.set(Calendar.DAY_OF_WEEK, Week + 1); // 按中国习惯，周一为第一天
        str = getFormatDate(cr.getTime());
        return str;
    }

    public static Long fomartMillis(String time) {
        if (time == null || time.isEmpty() || "0".equals(time)) {
            return System.currentTimeMillis();
        }
        if (time.length() > 13) {
            time = time.substring(0, 13);
        }
        return Long.valueOf(time);
    }

    public static void main(String[] args) {
        String time = null;
        System.out.println(fomartMillis(time));
    }

    /**
     * 返回格式化后的日期
     *
     * @param date
     * @return
     */
    public static String getFormatDate(Date date) {
        try {
            SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return bartDateFormat.format(date); // 返回格式化后的日期
        } catch (Exception ex) {
            return "";
        }
    }

    /**
     * 得到当前时间的年,月,日,时,分,秒,参数1:年,2:月,3:日,4:时,5:分,6:秒
     *
     * @param T_TYPE int
     * @return int
     */
    private static int getNow(int T_TYPE) {
        Calendar cr = Calendar.getInstance(); // 当时的日期和时间
        cr.setTime(new Date());
        int year   = cr.get(Calendar.YEAR);
        int month  = cr.get(Calendar.MONTH) + 1;
        int day    = cr.get(Calendar.DATE);
        int hour   = cr.get(Calendar.HOUR_OF_DAY);
        int minute = cr.get(Calendar.MINUTE);
        int second = cr.get(Calendar.SECOND);
        switch (T_TYPE) {
            case 1:
                return year;
            case 2:
                return month;
            case 3:
                return day;
            case 4:
                return hour;
            case 5:
                return minute;
            case 6:
                return second;
        }
        return 0;
    }


    /**
     * 查询今天0点0分0秒
     *
     * @param days 增加的天数可以为负数
     * @return String 返回查询开始时间，格式(yyyy-MM-dd 00:00:00)
     */
    public static String startDay(int days) {
        Calendar cr = Calendar.getInstance(); // 当时的日期和时间
        cr.setTime(new Date());
        cr.add(Calendar.DATE, days);
        int    month = cr.get(Calendar.MONTH) + 1;
        int    day   = cr.get(Calendar.DATE);
        String M     = (TimeFun.getNow(2) < 10 ? "0" + String.valueOf(month) : String.valueOf(month));
        String D     = (TimeFun.getNow(3) < 10 ? "0" + String.valueOf(day) : String.valueOf(day));
        return TimeFun.getNow(1) + "-" + M + "-" + D + " 00:00:00";
    }

    /**
     * 查询今天23点59分59秒
     *
     * @param days 增加的天数，可以为负数
     * @return String 返回查询结束时间，格式(yyyy-MM-dd 23:59:59)
     */
    public static String endDay(int days) {
        Calendar cr = Calendar.getInstance(); // 当时的日期和时间
        cr.setTime(new Date());
        cr.add(Calendar.DATE, days);
        int    year  = cr.get(Calendar.YEAR);
        int    month = cr.get(Calendar.MONTH) + 1;
        int    day   = cr.get(Calendar.DATE);
        String M     = (TimeFun.getNow(2) < 10 ? "0" + String.valueOf(month) : String.valueOf(month));
        String D     = (TimeFun.getNow(3) < 10 ? "0" + String.valueOf(day) : String.valueOf(day));
        return year + "-" + M + "-" + D + " 23:59:59";
    }

    /**
     * 查询本月第一天
     *
     * @return String 本月第一天，格式(yyyy-MM-dd 00:00:00)
     */
    public static String MMStartDay() {
        Calendar         cal   = Calendar.getInstance();
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        // 当前月的第一天
        cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
        Date beginTime = cal.getTime();
        return datef.format(beginTime) + " 00:00:00";
    }

    /**
     * 查询本月最后一天
     *
     * @return String 本月最后一天，格式(yyyy-MM-dd 23:59:59)
     */
    public static String MMEndDay() {
        Calendar         cal   = Calendar.getInstance();
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        // 当前月的最后一天
        cal.set(Calendar.DATE, 1);
        cal.roll(Calendar.DATE, -1);
        Date endTime = cal.getTime();
        return datef.format(endTime) + " 23:59:59";
    }

    /**
     * 查询指定月份第一天的第一秒
     *
     * @param MM 月份
     * @return String 指定月份第一天，格式(yyyy-MM-dd 00:00:00)
     */
    public static String MMStartDay(int MM) {
        Calendar         cal   = Calendar.getInstance();
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        // 当前月的第一天
        cal.set(GregorianCalendar.MONTH, MM - 1);
        cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
        Date beginTime = cal.getTime();
        return datef.format(beginTime) + " 00:00:00";
    }

    /**
     * 查询指定月份最后一天的最后一秒
     *
     * @param MM 月份
     * @return String 指定月份最后一天，格式(yyyy-MM-dd 00:00:00)
     */
    public static String MMEndDay(int MM) {
        Calendar         cal   = Calendar.getInstance();
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        // 指定月份的最后一天
        // 设置Calendar月份数为MM
        cal.set(Calendar.MONTH, MM);
        // 设置Calendar日期为下一个月一号
        cal.set(Calendar.DATE, 1);
        // 设置Calendar日期减一,为月末
        cal.add(Calendar.DATE, -1);
        Date endTime = cal.getTime();
        return datef.format(endTime) + " 23:59:59";
    }

    /**
     * 返回当前时间加指定天数后的日期
     *
     * @param days 指定天数,可为负数
     * @param time 后面的时间，例如23:59:59
     * @return String 指定天数后的日期，返回格式(yyyy-MM-dd 23:59:59)
     */
    public static String setDay(int days, String time) {
        Calendar         cal   = Calendar.getInstance();
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        // 指定月份的最后一天
        // 设置Calendar日期加天数
        cal.add(Calendar.DATE, +days);
        Date endTime = cal.getTime();
        return datef.format(endTime) + " " + time;
    }

    /**
     * 得到时间差,精确到毫秒
     *
     * @param preStr String
     * @param nowStr String
     * @return long
     */
    public static long getGapMSEL(String preStr, String nowStr) {
        Date pred = stringToDate(preStr);
        Date nowd = stringToDate(nowStr);
        return nowd.getTime() - pred.getTime();
    }

    /**
     * 得到时间差,精确到秒
     *
     * @param preStr String
     * @param nowStr String
     * @return long
     */
    public static long getGapSECOND(String preStr, String nowStr) {
        Date pred = stringToDate(preStr);
        Date nowd = stringToDate(nowStr);
        return (nowd.getTime() - pred.getTime()) / 1000;
    }

    /**
     * 得到时间差,精确到分
     *
     * @param preStr String
     * @param nowStr String
     * @return long
     */
    public static long getGapMENUTE(String preStr, String nowStr) {
        Date pred = stringToDate(preStr);
        Date nowd = stringToDate(nowStr);
        return (nowd.getTime() - pred.getTime()) / (1000 * 60);
    }

    /**
     * 得到时间差,精确到小时
     *
     * @param preStr String
     * @param nowStr String
     * @return long
     */
    public static long getGapHOUR(String preStr, String nowStr) {
        Date pred = stringToDate(preStr);
        Date nowd = stringToDate(nowStr);
        return (nowd.getTime() - pred.getTime()) / (1000 * 60 * 60);
    }

    /**
     * 得到时间差,精确到天
     *
     * @param preStr String
     * @param nowStr String
     * @return long
     */
    public static long getGapDAY(String preStr, String nowStr) {
        Date pred = stringToDate(preStr);
        Date nowd = stringToDate(nowStr);
        return (nowd.getTime() - pred.getTime()) / (1000 * 60 * 60 * 24);
    }

    /**
     * 得到当前系统时间加随机数
     *
     * @return String
     */
    public static String getDateRandom() {
        // 设置日期返回形式
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date             date           = new Date(); // 获取当前系统日期串
        int              i              = (int) (Math.random() * 900) + 100;
        return bartDateFormat.format(date) + String.valueOf(i); // 返回格式化后的日期
    }

    /**
     * 得到当前系统时间(yyyy-MM-dd HH:mm:ss)
     *
     * @return String
     */
    public static String getDate() {
        // 设置日期返回形式
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date             date           = new Date(); // 获取当前系统日期串
        return bartDateFormat.format(date); // 返回格式化后的日期
    }

    /**
     * 得到当前系统时间
     *
     * @param format yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getDate(String format) {
        // 设置日期返回形式
        SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);
        Date             date           = new Date(); // 获取当前系统日期串
        return bartDateFormat.format(date); // 返回格式化后的日期
    }

    /**
     * 根据时间戳获取当前格式化时间
     *
     * @return String
     */
    public static String getDate(Long datestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(datestamp));
    }

    /**
     * 字符变日期
     *
     * @return Date
     * @param:dateStr, 日期字符格式应为:Year-monthday
     * 日期字符格式应为:Year-month-day hh:mm:ss
     */
    private static Date stringToDate(String dateStr) {
        Timestamp         ts = Timestamp.valueOf(dateStr);
        GregorianCalendar g  = new GregorianCalendar();
        g.setTime(ts);
        return g.getTime();
    }

    /**
     * 根据参数格式返回当前日期,示例yyyyMMddHHmmss
     *
     * @param yyyyMMddHHmmss
     * @return
     */
    public static String formatTime(String yyyyMMddHHmmss) {
        Calendar         calendar = Calendar.getInstance();
        SimpleDateFormat sdf      = new SimpleDateFormat(yyyyMMddHHmmss);
        return sdf.format(calendar.getTime());
    }

    public static String formatTime(String yyyyMMddHHmmss, int HOUR, int MINUTE, int SECOND, int MILLISECOND) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, HOUR);
        calendar.set(Calendar.MINUTE, MINUTE);
        calendar.set(Calendar.SECOND, SECOND);
        calendar.set(Calendar.MILLISECOND, MILLISECOND);
        SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss);
        return sdf.format(calendar.getTime());
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty())
            format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long             l   = Long.valueOf(seconds + "000");
        return sdf.format(new Date(l));
    }

    /**
     * 日期格式字符串转换成10位时间戳
     *
     * @param date_str
     * @param format
     * @return
     */
    public static Long date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime() / 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /**
     * 获取传入日期是周1-7的那一天
     *
     * @param date
     * @return
     */
    public static int getWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index <= 0) {
            week_index = 7;
        }
        return week_index;
    }

    /**
     * 将秒数转换为日时分秒，
     *
     * @param second
     * @return
     */
    public static String secondToTime(long second) {
        long days = second / 86400;            //转换天数
        second = second % 86400;            //剩余秒数
        long hours = second / 3600;            //转换小时
        second = second % 3600;                //剩余秒数
        long minutes = second / 60;            //转换分钟
        second = second % 60;                //剩余秒数
        String hoursStr, minutesStr, secondStr;
        if (hours < 10) {
            hoursStr = "0" + hours;
        } else {
            hoursStr = String.valueOf(hours);
        }
        if (minutes < 10) {
            minutesStr = "0" + minutes;
        } else {
            minutesStr = String.valueOf(minutes);
        }
        if (second < 10) {
            secondStr = "0" + second;
        } else {
            secondStr = String.valueOf(second);
        }

        if (days > 0) {
            return days + ":" + hoursStr + ":" + minutesStr + ":" + secondStr;
        } else {
            return hoursStr + ":" + minutesStr + ":" + secondStr;
        }
    }
    public static Long date2ms(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime() ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }
}
